/*
*  培训记录
* 
*/
var trainprogresslistmodule = angular.module("trainprogress",['ui.tree','PensonSelector', 'tm.pagination','Common', 'loadModule', 'knowledgebaseS'])
.controller('trainprogresslist', ['$scope','$rootScope','sysUserInfo', 'RequestTrainService', 'Comm', 'MsgCon', 'Msg','$state', function ($scope,$rootScope,sysUserInfo, RTS, Comm, MsgCon, Msg,$state) {
    /*********************************************取出任务信息*************************************************/
        $scope.arrangeinfo =$rootScope.arrangeinfo; //传入培训任务的基本属性       
         $scope.trainTaskUrl = $rootScope.userurl;
    /**************************************初始化参数***********************************************************/
    $scope.List = [];
    $scope.params = {
            arrange_Id: $scope.arrangeinfo.id ,
            orgId:sysUserInfo.getuserinfo().organization_ID,
            user_id:"",//所有用户id
            org_Id:"",//部门id
            role_Id:"",//角色id
            user_groupId:"",//用户组id
            searchId: "",    
            searchType:"",
            username: "",
            isPublic:$scope.arrangeinfo.is_public, // 是否全员参与
            state:'2',    // 0 全部 1进行中 2 已完成 3 未完成
            course_count: $scope.arrangeinfo.courser_count,    //总课程数
            pageIndex:"1",
            pageSize:"10",
        };

     //传参部门，用户组，角色，用户
     $scope.initParams=function(){
        var list=JSON.parse($scope.arrangeinfo.arrage);
        if (list.hasOwnProperty('userList')){ 
            list.userList.forEach(function(data,index){
                $scope.params.user_id+=($scope.params.user_id.length<=0?"":",")+data.user_ID;
            });
        }if (list.hasOwnProperty('userOrganization')){
            list.userOrganization.forEach(function(data,index){
                $scope.params.org_Id+=($scope.params.org_Id.length<=0?"":",")+data.organization_ID;
            });
        }if (list.hasOwnProperty('userRole')){
            list.userRole.forEach(function(data,index){
                $scope.params.role_Id+=($scope.params.role_Id.length<=0?"":",")+data.roles_ID;
            });
        }if (list.hasOwnProperty('userGroup')){
            list.userGroup.forEach(function(data,index){
                $scope.params.user_groupId+=($scope.params.user_groupId.length<=0?"":",")+data.userGroup_ID;
            });
        }
     }
     $scope.initParams();

        $scope.succ=true;
        //分页
        $scope.paginationConf = {
            currentPage:1,//$location.search().currentPage ? $location.search().currentPage :
            totalItems:0,
            itemsPerPage: 10,
            //pagesLength: 10,
            perPageOptions: [10, 20],
            //rememberPerPage: 'perPageItems',
            onChange: function () {
            if (!$scope.succ) {
                Msg.info("正在查询中！");
                return;
            } else {
                if( (parseInt($scope.params.pageIndex) !=parseInt($scope.paginationConf.currentPage))||(parseInt($scope.params.pageSize) !=parseInt($scope.paginationConf.itemsPerPage))){
                        console.log(parseInt($scope.params.pageIndex) !=parseInt($scope.paginationConf.currentPage));
                        if($scope.paginationConf.currentPage==0){$scope.paginationConf.currentPage=1;}
                        $scope.params.pageIndex =$scope.paginationConf.currentPage;
                        $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                        $scope.GetProgressList();
                    }
                }
            }
        }
        //左侧选择器初始化
        $scope.ApiData={
	        windowModal:"3",//调用模式 1弹窗模式 2宽度大于450 3 宽度小于450
	        ViceOrgOpen:false,//HtmlOrg弹框样式 关闭 选择器再次引用选择器样式
	        ViceGroupOpen:false,//HtmlOrg弹框样式 关闭 选择器再次引用选择器样式
	        ViceRoleOpen:false,//HtmlOrg弹框样式 关闭 选择器再次引用选择器样式
            ApiOpen:true,//为true时 页面打开选择器 false 关闭控制器
                 menu:[//进入人员选择器的菜单         
                    { "id":"Org"},
                    {"id":"Group"},
                    {"id":"Role"}
                 ],
                "ViceuserRoleList": [//传入的角色列表 再次点击 查询 双向绑定不能影响数据源

                ],
                "ViceuserOrgList": [//传入的部门列表 再次点击 查询 双向绑定不能影响数据源
                
                ],
                "ViceuserGroupList": [//传入的用户组 再次点击 查询 双向绑定不能影响数据源

                ],
            userOrgList:[],
            userRoleList:[],
            userGroupList:[]
        };
        //声明唯一部门对象
        $scope.OrgObj={
            organization_ID:"",
            organization_Name:"",
            parentId:"",
            parentName:"",
            organization_Num:0,
            organization_Sort:"",
            organization_No:"",
            orgOutsidePhone:"",
            orgHousePhone:"",
            Phone:""
        };     
        //声明唯一角色对象
        $scope.RolesObj={
            roles_ID:"",
            roles_Name:"",
            parentId:"",
            parentName:"",
            roles_Num:0,
            roles_Sort:"",
            roles_No:"",            
        };     
        //声明唯一角色对象
        $scope.GroupObj={
            userGroup_ID:"",
            userGroup_Name:"",
            parentId:"",
            parentName:"",
            userGroup_Num:0,
            userGroup_Sort:"",
            userGroup_No:"",            
        };     
        //获取选择器所选对象
        $scope.GetSelectModal=function(PublicList,obj){
                try {
                       if (PublicList.length>0) {
                         return  obj= PublicList[0];
                     }
                } catch(e) {
                    console.log(e);
                }
        }     
        //由于人员选择器设计多选 在此处调用需要进行数据独立
        $scope.OnlyOne=function(PublicList){
            if (PublicList.length>1) {
                PublicList.splice(0, 1);
            }
        }

        $scope.searchType=0;//0,全部，1组织架构，3角色，2用户组
        $scope.OrgObj={};
        $scope.RolesObj={};
        $scope.GroupObj={};
         //页面监听部门左侧列表选项
        var watch = $scope.$watch('ApiData.userOrgList',function(newValue,oldValue, scope){
            if(newValue==oldValue){
                return;
            }
            $scope.OnlyOne($scope.ApiData.userOrgList);//排除集合对象 
            $scope.OrgObj=$scope.ApiData.userOrgList[0];//取单一         
            $scope.params.pageIndex = "1";    
            $scope.searchType=1;
            $scope.GetProgressList($scope.OrgObj,1);                    
        },true);
        //--- 监听角色
        var watch = $scope.$watch('ApiData.userRoleList',function(newValue,oldValue, scope){
            if(newValue==oldValue){
                return;
            }
            $scope.OnlyOne($scope.ApiData.userRoleList);//排除集合对象 
            $scope.RolesObj=$scope.ApiData.userRoleList[0];//取单一            
            $scope.params.pageIndex = "1";       
            $scope.searchType=3;
            $scope.GetProgressList($scope.RolesObj,3);    
        },true);
        //--- 监听用户组
        var watch = $scope.$watch('ApiData.userGroupList',function(newValue,oldValue, scope){
            if(newValue==oldValue){
                return;
            }
            $scope.OnlyOne($scope.ApiData.userGroupList);//排除集合对象 
            $scope.GroupObj=$scope.GetSelectModal($scope.ApiData.userGroupList,$scope.GroupObj);//取单一              
            $scope.params.pageIndex = "1";     
            $scope.searchType=2;
            $scope.GetProgressList($scope.GroupObj,2);
        },true);
       // 通过状态筛选培训进度 
       $scope.handleWhere = function(state){
            $scope.params.pageIndex = "1";
            $scope.params.state = state;
            $scope.trainStatistic = false;
            $scope.GetProgressList();
       }
       //知识选择器查询
       $scope.GetProgressList=function(){
            $scope.succ=false;
            $scope.isloading=true;
            if($scope.searchType==1){//部门
                $scope.params.searchId =$scope.OrgObj.organization_ID;
                $scope.params.searchType=1; 
            }else if($scope.searchType==2){//用户组
                 $scope.params.searchId =$scope.GroupObj.userGroup_ID;
                $scope.params.searchType=2; 
            }else if($scope.searchType==3){//角色
                $scope.params.searchId =$scope.RolesObj.roles_ID;
                $scope.params.searchType=3;  
            }else{
                $scope.params.searchId ="";
                $scope.params.searchType=""; 
            }
            console.log($scope.params);
            RTS.findArrangeProgress($scope.params).then(function(data){
                    $scope.succ=true;
                    $scope.isloading=false;      
                    if (data.errorcode == "0") {
                        $scope.List=data.datas;
                        if ($scope.List.length<=0) {
                            return;
                        }else {
                            $scope.List.forEach(function(obj,index){
                                obj.sDate=obj.study_Date;
                                obj.eDate=obj.last_Date;
                            })
                           $scope.paginationConf.totalItems = data.numCount;
                           $scope.paginationConf.currentPage = data.pageIndex;  
                       }
                    }else{
                       Msg.error("请求数据有误");
                    }
            });
       }
    $scope.gobackexam = function(){
       $state.go("index.Train.task"); 
    }
   //搜索查询回车
    $scope.findlistbyEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
//            console.log('执行了回车');
            $scope.params.pageIndex = "1";
            $scope.GetProgressList();
        }
    }
    //搜索查询按钮
    $scope.findlistbybutton = function () {
        $scope.params.pageIndex = "1";
        $scope.searchType=0;
        $scope.GetProgressList();
    }
    /********************************查看个人记录*********************************/
    // 显示隐藏
    $scope.recoreShow = false;
    // title 文字
    $scope.recoreTitle = "";
    // 打开人员的个人记录
    $scope.openRecoreList = function(item){
        console.log(item);
        // 名称
        $scope.recoreTitle = item.user_Name;
        $scope.recoreShow = true;
    }
    // 关闭个人记录
    $scope.closeRecoreList = function(){
        $scope.recoreShow = false;
    }
  
    /****************************导出**************************************/
     $scope.loading = true;
     $scope.excelparam={arrangeid: $scope.arrangeinfo.id,orgId:sysUserInfo.getuserinfo().organization_ID,arrangeName:$scope.arrangeinfo.name,isPublic:$scope.arrangeinfo.is_public, course_count: $scope.arrangeinfo.courser_count};
	$scope.excelparam.znOren="员工编号,学员名称,账号,手机号,邮箱,最后学习日期,完成度,组织架构,任务总学时,完成学时";
    //请求人员导出
        $scope.downloadExcel=function(){
            RTS.taskExport($scope.excelparam).then(function(data){                          
                    $scope.loading = false;
                    if (data.errorcode == "0") {
                        window.location = data.errormsg;
                    }
                    else if (data.errorcode == "73") {
                        setTimeout(function () {
                            $scope.downloadExcel();
                        }, 5000);
                        $scope.loading = true;
                        Msg.warning("正在统计请稍后..");
                    } else {
                        Msg.error("导出失败 ！");
                    }                                  
                }, function callback() {                     
                    $scope.loadingflag = false;           
                });    
        }

        $scope.showUpdateTime=false;
        $scope.upCount=0;

        $scope.updateCount=function(){
            $scope.upCount++;
            if($scope.upCount>=5){
                $scope.showUpdateTime=true;
            }
        }
        var reDateTime = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
        //失焦保存
        $scope.blurSave=function(obj,type){
            var time=obj.sDate;
            if(type==2){
                time=obj.eDate;
                if(time==obj.last_Date){
                    return;
                }
            }else{
                if(time==obj.study_Date){
                    return;
                }
            }
            console.log("不一样")
            if(reDateTime.test(time)){

                RTS.modifyUserStudy({xid:obj.progressId,xtype:2,startTime:(type==1?time:''),endTime:(type==2?time:''),uid:sysUserInfo.getuserinfo().user_ID}).then(function(data){                          
                    if (data.errorcode == "0") {
                        if(type==1){
                            obj.study_Date=time;
                        }else{
                            obj.last_Date=time;
                        }
                        Msg.success("修改成功")
                    }else if (data.errorcode != "6") {
                        Msg.error(data.errormsg);
                    } else {
                        Msg.error("修改失败 ！");
                    }                                  
                }, function callback() {                     
                    Msg.error("修改异常！");
                });  
            }else{
                Msg.warning("请输入合法的日期！");
            }
        }
} ]);








